package com.meihua.baoleyi.ui;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.meihua.baoleyi.R;
import com.meihua.baoleyi.base.BaseActivity;
import com.meihua.baoleyi.net.PwdClass;
import com.meihua.baoleyi.utils.CountDownTimerUtils;
import com.meihua.baoleyi.utils.ToastUtil;
import com.meihua.baoleyi.utils.ToastUtils;
import com.meihua.baoleyi.utils.UrlContants;
import com.meihua.baoleyi.utils.ValidateHelper;
import com.meihua.baoleyi.view.ForgetPwdView;

import org.json.JSONException;
import org.json.JSONObject;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

/**
 * Name: liming
 * Time: 2017/10/16.
 * Dec:找回密码
 */

public class FindPwdActivity extends BaseActivity implements ForgetPwdView, View.OnClickListener {
    @Bind(R.id.center_text)
    TextView centerText;
    @Bind(R.id.left_img)
    RelativeLayout leftImg;
    @Bind(R.id.right_text)
    TextView rightText;

    private EditText editText, forget_code;
    private Toast toast;
    private Button forget_send_code;
    String phoneNum;
    String code;
    private PwdClass mForgetPresenter;

    public boolean isEmpty(String str) {
        return str == null || "".equals(str.trim()) || "null".equals(str);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actiivty_find_pwd);
        ButterKnife.bind(this);
        editText = (EditText) findViewById(R.id.editText);
        forget_code = (EditText) findViewById(R.id.forget_code);
        forget_send_code = (Button) findViewById(R.id.forget_send_code);
        forget_send_code.setOnClickListener(this);
        findViewById(R.id.forget_pwd_submit).setOnClickListener(this);
        rightText.setVisibility(View.GONE);
        centerText.setText("忘记密码");
    }

    @Override
    public void onForget(String username, String code, String msg, boolean isSuccess) {
        if (isSuccess) {
            startActivity(new Intent(this, ResetPwdActivity.class));
            finish();
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void sendCode(String code, boolean isSuccess, String msg) {
        if (isSuccess) {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.forget_send_code:
                sendSmsCode();
                break;
            case R.id.forget_pwd_submit:
                phoneNum = editText.getText().toString().trim();
                code = forget_code.getText().toString().trim();
//                mForgetPresenter.forget(phone, code);
                click_submit();//提交
                break;

        }
    }

    private void click_submit() {
        OkGo.get(UrlContants.FORGET_PWD)
                .tag(this)
                .params("user_phone", phoneNum)
                .params("code", code)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject jsonObject = new JSONObject(s);
                            if (jsonObject.optString("code").equals("200")) {
//                                        ToastUtil.showToast(FindPwdActivity.this,jsonObject.optString("msg"), 200);
                                Intent intent = new Intent(FindPwdActivity.this, ResetPwdActivity.class);
                                intent.putExtra("user_phone", phoneNum);
                                startActivity(intent);
                            } else {
                                ToastUtil.show(FindPwdActivity.this, jsonObject.optString("msg"));
                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);

                    }
                });
    }

    public void showShortCenter(Context context, int content) {
        if (toast == null) {
            toast = Toast.makeText(context, content, Toast.LENGTH_SHORT);
            toast.setGravity(Gravity.CENTER, 0, 0);
        } else {
            toast.setText(content);
            toast.setGravity(Gravity.CENTER, 0, 0);
        }
        toast.show();
    }

    /**
     * 获取验证码
     */
    public void sendSmsCode() {
        if (isEmpty(editText.getText().toString().trim())) {
            showShortCenter(this, R.string.input_phone);
            return;
        }
        CountDownTimerUtils countDownTimerUtils = new CountDownTimerUtils(FindPwdActivity.this, forget_send_code, 60000, 1000);
        countDownTimerUtils.start();
        phoneNum = editText.getText().toString().trim();
        boolean result = ValidateHelper.isPhoneNumberValid(phoneNum);
        if (!result) {
            Toast.makeText(this, "请输入正确的手机号", Toast.LENGTH_SHORT).show();
            return;
        }
        OkGo.get(UrlContants.FORGET_PWD_SEND_CODE)
                .tag(this)
                .params("phone", phoneNum)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        if (s != null) {
                            try {
                                JSONObject jsonObject = new JSONObject(s);
                                if (jsonObject.optString("code").equals("200")) {
                                    ToastUtils.showT(FindPwdActivity.this, "发送验证码成功！");
                                } else {
                                    String message = jsonObject.getString("msg");
                                    ToastUtils.showT(FindPwdActivity.this, message);
                                }
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }
                        }
                    }

                    @Override
                    public void onError(Call call, Response response, Exception e) {
                        super.onError(call, response, e);
                    }
                });
    }

    @OnClick(R.id.title_iv_back)
    public void onViewClicked() {
        finish();
    }
}
